CODEDIGEST
Home » CodeDigest
Search
 

Technologies
 

What is null-coalescing operator[??] in C#?
Submitted By Satheesh Babu B
On 4/8/2010 7:31:01 AM
Tags: C#,CodeDigest  

What is null-coalescing operator[??] in C#?


The ?? is called null-coalescing operator which can be used to assign a default value when a nullable type is assigned to a non nullable type.
Refer the below code to understand better,

 

         int? n1;
        //Do some operations   
        int finalvalue = (n1 ?? 10);

 

In the above code, finalvalue will be assigned with 10 if n1 is null else it will be assigned with the value of n1. In short, finalvalue will hold the left-handside operand if it is not null else it will hold 10.

 

Also, by default you will not able to assign a nullable type to a non nullable type. In this case, you can use ?? operator by specifying a default value.

 

Happy Coding!!


 

Do you have a working code that can be used by anyone? Submit it here. It may help someone in the community!!

Recent Codes
  • View All Codes..